package com.cpi.cornpp.purchaseorder.comparator;

import java.util.Comparator;

import com.cpi.cornpp.biweek.comparator.BiWeekComparator;
import com.cpi.cornpp.biweeks.view.BiWeekPeriodVersionView;
import com.cpi.cornpp.plants.view.PlantView;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderVersionView;

public class PurchaseOrderVersionComparator implements Comparator {
	public static final int PURCHASE_ORDER_VERSION_BI_WEEK_PERIOD_FIELD = 3;
	public static final int PURCHASE_ORDER_VERSION_PLANT_FIELD = 4;
	public static final int PURCHASE_ORDER_VERSION_CODE_FIELD = 5;
	public static final int PURCHASE_ORDER_VERSION_CAPACITY_FIELD = 6;
	
	private int orderField;
	private boolean orderAsc;
	
	public int getOrderField() {
		return orderField;
	}
	public void setOrderField(int aOrderField) {
		orderField = aOrderField;
	}	
	public boolean isOrderAsc() {
		return orderAsc;
	}
	public void setOrderAsc(boolean aOrderAsc) {
		orderAsc = aOrderAsc;
	}
	
	public int compare(Object object, Object objectToCompare) {
		int result = -0;
		PurchaseOrderVersionView povView = null;
		PurchaseOrderVersionView povViewToCompare = null;
		BiWeekPeriodVersionView bpvView = null;
		BiWeekPeriodVersionView bpvViewToCompare = null;
		PlantView plantView = null;
		PlantView plantViewToCompare = null;
		BiWeekComparator biWeekComparator = null;
		
		if ((object != null) && (objectToCompare != null) && (object instanceof PurchaseOrderVersionView) && (objectToCompare instanceof PurchaseOrderVersionView)){
			povView = (PurchaseOrderVersionView) object;
			povViewToCompare = (PurchaseOrderVersionView) objectToCompare;
			
			switch (orderField){
				case 3:
					bpvView = povView.getBiWeekPeriodVersionView();
					bpvViewToCompare = povViewToCompare.getBiWeekPeriodVersionView();
					
					if ((bpvView != null) && (bpvViewToCompare != null)){
						
						if ((bpvView.getBiWeekView() != null) && (bpvViewToCompare.getBiWeekView() != null) && (bpvView.getBiWeekView() != null) && (bpvViewToCompare.getBiWeekView() != null)){
							biWeekComparator = new BiWeekComparator();
							result = biWeekComparator.compare(bpvView.getBiWeekView(), bpvViewToCompare.getBiWeekView());
						} else if (bpvView.getBiWeekView() == null){
							result = -1;
						} else {
							result = 1;
						}						
					}
				break;
				case 4:
					plantView = povView.getPlantView();
					plantViewToCompare = povViewToCompare.getPlantView();
					
					if ((plantView != null) && (plantViewToCompare != null)){
						
						if ((plantView.getShortName() != null) && (plantViewToCompare.getShortName() != null)){
							result = plantView.getShortName().compareTo(plantViewToCompare.getShortName());
						} else if (plantView.getShortName() == null){
							result = -1;
						} else {
							result = 1;
						}
					} else if (plantView == null){
						result = -1;
					} else {
						result = 1;
					}
				break;
				case 5:
					if ((povView.getCode() != null) && (povViewToCompare.getCode() != null) && (povView.getPostFix() != null) && (povViewToCompare.getPostFix() != null)){
						result = ( povView.getCode() + povView.getPostFix()).compareTo(( povViewToCompare.getCode() + povViewToCompare.getPostFix() ));
					} else if (povView.getCode() == null){
						result = -1;
					} else {
						result = 1;
					}
					
				break;
				case 6:
					if ((povView.getCapacity() != null) && (povViewToCompare.getCapacity() != null)){
						result = povView.getCapacity().compareTo(povViewToCompare.getCapacity());
					} else if (povView.getCapacity() == null){
						result = -1;
					} else {
						result = 1;
					}
					
				break;
				default:
					break;
			}
			
			if (orderAsc){
				result *= -1;
			}
		}
		
		return result;
	}
}
